﻿using ConfExpert.Utility;
using ConfOrderDS.AppDSTableAdapters;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using Extensions;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfOrder
{
    public partial class OrderRefs : XForm
    {
        COOrderRefTableAdapter ta = new COOrderRefTableAdapter();

        public OrderRefs()
        {
            InitializeComponent();
        }

        BindingSource bsOrderRef;
        private void Style_Load(object sender, EventArgs e)
        {
            RepositoryManager.dsi.COOrderRef.Clear();
            AppSession.DataManager.Fill(RepositoryManager.dsi, "COOrderRef", "COGetOrderRef", AppSession.UserInfo.CustomerId);

            bsOrderRef = new BindingSource(RepositoryManager.dsi, "COOrderRef");

            InitGridView(gvStyle, bsOrderRef);
            gvStyle.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            RepositoryManager.dsi.COOrderRef.TableNewRow += COOrderRef_TableNewRow;

            gvStyle.Columns["OrderRefId"].OptionsColumn.AllowEdit = false;
            gvStyle.Columns["OrderRefId"].AppearanceCell.BackColor = System.Drawing.Color.LightYellow;

            gvStyle.FormatAsNumber(0, "CurrentQty", "EstimatedQty");
            gvStyle.AddSummary("CurrentQty", "EstimatedQty");
            gvStyle.SetCaptions("SezonId:Season");
            gvStyle.MakeColumnInvisible("CustomerId", "AyrimId", "CurrentQty");
            gvStyle.CellValueChanged += gvStyle_CellValueChanged;
            gvStyle.BestFitColumns();
            MainToolbar.Visible = true;

            //this.Deactivate += OrderRef_Deactivate;
            gvStyle.OptionsView.EnableAppearanceOddRow = true;
        }

        void OrderRef_Deactivate(object sender, EventArgs e)
        {
            gvStyle.CloseEditor();
            bsOrderRef.EndEdit();
            DataTable dt = RepositoryManager.dsi.COOrderRef.GetChanges();
            if (dt != null)
                bbiSave.PerformClick();
        }

        void COOrderRef_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["CustomerId"] = AppSession.UserInfo.CustomerId;
        }

        void gvStyle_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            gvStyle.BestFitColumns();
        }

        private void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvStyle.CloseEditor();
            bsOrderRef.EndEdit();

            DataTable d = RepositoryManager.dsi.COOrderRef;

            if (RepositoryManager.dsi.COOrderRef.Select("isnull(OrderRef,'') = ''").Length > 0)
            {
                XtraMessageBox.Show("Order Reference can not be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (RepositoryManager.dsi.COOrderRef.Select("isnull(SezonId,0) = 0").Length > 0)
            {
                XtraMessageBox.Show("Season can not be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string orderRef = RepositoryManager.dsi.COOrderRef.SerializeChanges();
            //if (orderRef != "")
            //    AppSession.DataManager.ExecuteNonQuery("COInsLog", "OrderRef", orderRef);

            ta.Update(RepositoryManager.dsi.COOrderRef);

            gvStyle.BestFitColumns();
            Notify("Saved successfully.");
        }
    }
}